package com.dotmedia.dboperations;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dotmedia.core.DatabaseAccessService;
import com.dotmedia.entities.AdTemplate;

/**
 * This class deals with the database operations for the AdTemplate entity
 * 
 * @author siddharthsaha
 * 
 */
public class AdTemplateDatabaseOperations {

	private static Logger logger = Logger.getLogger("dboperations");

	public void persistAdTemplateEntity(AdTemplate adTemplateEntity) {
		logger.info("IN method persistAdTemplateEntity() in AdTemplateDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int persistStatus = databaseAccessService.persistEntity(adTemplateEntity);
		if (persistStatus == 0) {
			logger.info("The AdTemplate entity was successfully persisted");
		} else {
			logger.error("The AdTemplate entity was not successfully persisted");
		}
		logger.info("OUT method persistAdTemplateEntity() in AdTemplateDatabaseOperations");
	}

	public void removeAdTemplateEntity(AdTemplate adTemplateEntity) {
		logger.info("IN method removeAdTemplateEntity() in AdTemplateDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int removeStatus = databaseAccessService.removeEntity(adTemplateEntity, adTemplateEntity.getAdtplId().intValue());
		if (removeStatus == 0) {
			logger.info("The AdTemplate entity was successfully removed from the database");
		} else {
			logger.error("The AdTemplate entity was not successfully removed from the database");
		}
		logger.info("OUT method removeAdTemplateEntity() in AdTemplateDatabaseOperations");
	}

	public void updateAdTemplateEntity(AdTemplate adTemplateEntity) {
		logger.info("IN method updateAdTemplateEntity() in AdTemplateDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		String query = "SELECT adtplEntity FROM AdTemplate adtplEntity WHERE adtplEntity.system.id = " + adTemplateEntity.getSystem().getId()
				+ " AND adtplEntity.adtplRefCode = '" + adTemplateEntity.getAdtplRefCode() + "'";
		logger.info("Query: " + query);
		List retrivedEntities = databaseAccessService.getEntities(query);
		boolean updated = false;
		for (int index = 0; index < retrivedEntities.size(); index++) {
			AdTemplate currentAdTemplateEntity = (AdTemplate) retrivedEntities.get(index);
			if (currentAdTemplateEntity.getSystem().getId().intValue() == adTemplateEntity.getSystem().getId().intValue()
					&& currentAdTemplateEntity.getAdtplRefCode().equals(adTemplateEntity.getAdtplRefCode())) {
				logger.info("Obtained the AdTemplate entity to be updated");
				currentAdTemplateEntity.setCode(adTemplateEntity.getCode());
				currentAdTemplateEntity.setDescription(adTemplateEntity.getDescription());
				currentAdTemplateEntity.setStatus(adTemplateEntity.getStatus());
				int updateStatus = databaseAccessService.updateEntity(currentAdTemplateEntity);
				if (updateStatus == 0) {
					updated = true;
				}
			}
			break;
		}
		if (updated) {
			logger.info("The AdTemplate entity was successfully updated");
		} else {
			logger.error("The AdTemplate entity was not successfully updated");
		}
		logger.info("OUT method updateAdTemplateEntity() in AdTemplateDatabaseOperations");
	}

	public List<AdTemplate> getAllAdTemplateEntities() {
		logger.info("IN method getAllAdTemplateEntities() in AdTemplateDatabaseOperations");
		List<AdTemplate> toReturnList = new ArrayList<AdTemplate>();
		String query = "SELECT adtplsEntity FROM AdTemplate adtplsEntity";
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedEntities.size(); index++) {
			AdTemplate currentAdTemplateEntity = (AdTemplate) retrievedEntities.get(index);
			toReturnList.add(currentAdTemplateEntity);
		}
		logger.info("OUT method getAllAdTemplateEntities() in AdTemplateDatabaseOperations");
		return toReturnList;
	}

	public List<AdTemplate> getAdTemplateEntitiesByAdTemplateId(int adTemplateId) {
		logger.info("IN method getAdTemplateEntitiesByAdTemplateId() in AdTemplateDatabaseOperations");
		List<AdTemplate> toReturnList = new ArrayList<AdTemplate>();
		String query = "SELECT adtplsEntity FROM AdTemplate adtplsEntity WHERE adtplsEntity.adtplId = " + adTemplateId;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedEntities.size(); index++) {
			AdTemplate currentAdTemplateEntity = (AdTemplate) retrievedEntities.get(index);
			if (currentAdTemplateEntity.getAdtplId().intValue() == adTemplateId) {
				toReturnList.add(currentAdTemplateEntity);
			}
		}
		logger.info("OUT method getAdTemplateEntitiesByAdTemplateId() in AdTemplateDatabaseOperations");
		return toReturnList;
	}

	public List<AdTemplate> getAdTemplateEntitiesByAdTemplateRefCode(String adTemplateRefCode) {
		logger.info("IN method getAdTemplateEntitiesByAdTemplateRefCode() in AdTemplateDatabaseOperations");
		List<AdTemplate> toReturnList = new ArrayList<AdTemplate>();
		String query = "SELECT adtplsEntity FROM AdTemplate adtplsEntity WHERE adtplsEntity.adtplRefCode = '" + adTemplateRefCode + "'";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrivedList = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrivedList.size(); index++) {
			AdTemplate currentAdTemplateEntity = (AdTemplate) retrivedList.get(index);
			if (currentAdTemplateEntity.getAdtplRefCode().equals(adTemplateRefCode)) {
				toReturnList.add(currentAdTemplateEntity);
			}
		}
		logger.info("OUT method getAdTemplateEntitiesByAdTemplateRefCode() in AdTemplateDatabaseOperations");
		return toReturnList;
	}

}
